package assignments.sagarjoshi.springtms;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ListContainer.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class ListContainer extends Fragment implements ListView.OnItemClickListener
{
    public ListContainer()   {/*Required empty public constructor*/ }

    public interface OnFragmentInteractionListener
    {

        public void onFragmentItemClicked(int data);
    }

    private OnFragmentInteractionListener mListener;
    ListView list;
    Context context;
    String[] projection, args;
    boolean flag;
    String selection,order,tablename;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        context=getActivity();
        View v= inflater.inflate(R.layout.listfragment_layout,container,false);
        Bundle b=getArguments();
        String query=b.getString("query");
        flag=b.getBoolean("flag");
        list=(ListView)v.findViewById(R.id.itemlist);
        list.setOnItemClickListener(this);
        setlist(query);

        return  v;
    }

    protected  void setlist(String query)
    {
        ArrayAdapter adapter;
        MyDatabaseHelper helper = new MyDatabaseHelper(getActivity());
        SQLiteDatabase mydb = helper.getReadableDatabase();
        Cursor cursor;
        int count;
        projection=null;
        query="%"+query+"%";

        if(!flag)
        {
            tablename = MyDatabaseHelper.DATABASE_TABLE_COURSE;
            selection=MyDatabaseHelper.KEY_COURSE_NAME + " LIKE ? OR " + MyDatabaseHelper.KEY_COURSE_DESCRIPTION + " LIKE ? OR " + MyDatabaseHelper.KEY_COURSE_MENTOR_NAME + " LIKE ?" ;
            args= new String[]{query,query,query};
            order = MyDatabaseHelper.KEY_COURSE_NAME+" ASC";

        }
        else
        {
            tablename = MyDatabaseHelper.DATABASE_TABLE_TRAINEE;
            selection = MyDatabaseHelper.KEY_TRAINEE_NAME + " LIKE ? OR " + MyDatabaseHelper.KEY_TRAINEE_COLLEGE_NAME + " LIKE ? OR " + MyDatabaseHelper.KEY_TRAINEE_TECHNOLOGY + " LIKE ?" ;
            args= new String[]{query,query,query};
            order = MyDatabaseHelper.KEY_ID;

        }

        cursor = mydb.query(tablename,projection,selection,args,null,null,order);
        count=cursor.getCount();

        if(cursor==null || count==0)
        {

            Toast.makeText(getActivity(),"Data could not be accessed or found",Toast.LENGTH_LONG).show();
            return;
        }

         if(!flag)
         {
             Course[] data = new Course[count];
             for(int i=0;i<count;i++)
             {
                 cursor.moveToPosition(i);
                 data[i]=new Course(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
             }
             adapter = new CourseAdapter(context,data);
         }

        else
         {
             Trainee[] data= new Trainee[count];
             for(int i=0;i<count;i++)
             {
                 cursor.moveToPosition(i);
                 data[i]= new Trainee(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6));
             }
             adapter=new TraineeAdapter(context,data);
         }

        list.setAdapter(adapter);

    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {

        int item_id;
        if(!flag)
        {
            Course course=(Course)parent.getItemAtPosition(position);
            item_id=course.getId();
        }
        else
        {
            Trainee trainee = (Trainee)parent.getItemAtPosition(position);
            item_id=trainee.getId();
        }
        mListener.onFragmentItemClicked(item_id);
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }



}
